package state.component;

import state.GameConstants;
import state.GameGlobal;
import state.Graphics;
import state.Image;
import state.Key;
import state.Manager;
import state.Sprite;
import state.TouchScreenListener;
import state.util.ImageUtil;
import development.Constants;

public class TouchPad extends Sprite implements TouchScreenListener {
  private Image[] touchImages;
  private Image[] touchBoldImages;
  
  private Sprite[] touchSprites;
  private int[] keyCodes;
  
  public TouchPad(Manager manager) {
    this.manager = manager;
    touchImages = new Image[5];
    touchImages[0] = ImageUtil.getCoreImage("Arrow_touch.png").setStatic(true);
    touchImages[1] = Image.createImage(touchImages[0], 0, 0, touchImages[0].getWidth(), touchImages[0].getHeight(), javax.microedition.lcdui.game.Sprite.TRANS_ROT90).setStatic(true);
    touchImages[2] = Image.createImage(touchImages[1], 0, 0, touchImages[1].getWidth(), touchImages[1].getHeight(), javax.microedition.lcdui.game.Sprite.TRANS_ROT90).setStatic(true);
    touchImages[3] = Image.createImage(touchImages[2], 0, 0, touchImages[2].getWidth(), touchImages[0].getHeight(), javax.microedition.lcdui.game.Sprite.TRANS_ROT90).setStatic(true);
    touchImages[4] = ImageUtil.getCoreImage("fireButton_touch.png").setStatic(true);
    
    touchBoldImages = new Image[5];
    touchBoldImages[0] = ImageUtil.getCoreImage("Arrow_touch_Bold.png").setStatic(true);
    touchBoldImages[1] = Image.createImage(touchBoldImages[0], 0, 0, touchBoldImages[0].getWidth(), touchBoldImages[0].getHeight(), javax.microedition.lcdui.game.Sprite.TRANS_ROT90).setStatic(true);
    touchBoldImages[2] = Image.createImage(touchBoldImages[1], 0, 0, touchBoldImages[1].getWidth(), touchBoldImages[1].getHeight(), javax.microedition.lcdui.game.Sprite.TRANS_ROT90).setStatic(true);
    touchBoldImages[3] = Image.createImage(touchBoldImages[2], 0, 0, touchBoldImages[2].getWidth(), touchBoldImages[0].getHeight(), javax.microedition.lcdui.game.Sprite.TRANS_ROT90).setStatic(true);
    touchBoldImages[4] = ImageUtil.getCoreImage("fireButton_touch_bold.png").setStatic(true);
    
    touchSprites = new Sprite[5];
    touchSprites[0] = new Sprite(touchImages[0], GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2 - 30, Constants.BOTTOM_HCENTER_ANCHOR);
    touchSprites[1] = new Sprite(touchImages[1], GameConstants.SCREEN_WIDTH / 2 + 30, GameConstants.SCREEN_HEIGHT / 2, Constants.VCENTER_LEFT_ANCHOR);
    touchSprites[2] = new Sprite(touchImages[2], GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2 + 30, Constants.TOP_HCENTER_ANCHOR);
    touchSprites[3] = new Sprite(touchImages[3], GameConstants.SCREEN_WIDTH / 2 - 30, GameConstants.SCREEN_HEIGHT / 2, Constants.VCENTER_RIGHT_ANCHOR);
    touchSprites[4] = new Sprite(touchImages[4], GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, Constants.CENTER_ANCHOR);
    
    for (int i = 0; i < touchSprites.length; i++) {
      touchSprites[i].setId((byte) i);
    }
    
    keyCodes = new int[] {Key.UP, Key.RIGHT, Key.DOWN, Key.LEFT, Key.FIRE};
    setTouchScreenListener(this);
  }
  
  public Sprite paint(Graphics g) {
    g.setClip(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
    for (int i = 0; i < touchSprites.length; i++) {
      touchSprites[i].paint(g);
    }
    return this;
  }

  public void pointerPressed(Sprite source, int x, int y) {
    for (int i = 0; i < touchSprites.length; i++) {
      if (touchSprites[i].contains(x, y)) {
        GameGlobal.systemCanvas.getKeyRepeatHandle().keyPressed(keyCodes[i]);
        touchSprites[i].setImage(touchBoldImages[i]);
        manager.stopEvent();
        manager.getParent().doKeyPressed(keyCodes[i]);
        break;
      }
    }
  }

  public void pointerReleased(Sprite source, int x, int y) {
    for (int i = 0; i < touchSprites.length; i++) {
      if (touchSprites[i].contains(x, y)) {
        GameGlobal.systemCanvas.getKeyRepeatHandle().keyReleased();
        touchSprites[i].setImage(touchImages[i]);
        manager.stopEvent();
        Key.isCommandKey = true;
        manager.getParent().doKeyReleased(keyCodes[i]);
        break;
      }
    }
  }

  public void pointerDragged(Sprite source, int x, int y) {
  }
  
  public Sprite detroy() {
    for (int i = 0; i < touchSprites.length; i++) {
      touchSprites[i].detroy();
    }
    return super.detroy();
  }
}
